package com.jiesen.base.modules.login.jsRole.mapper;

import com.jiesen.base.baseDao.BaseMapper;
import com.jiesen.base.modules.login.jsRole.model.JsRole;
import org.apache.ibatis.annotations.*;

import java.io.Serializable;
import java.util.List;

@Mapper
public interface JsRoleDao extends BaseMapper<JsRole> {

    //根据用户id查询用户所拥有的角色
    @Select("select `id`,`sort`,`name`,`lock`,`description`,`branch_id`,`updatetime`,`addtime` " +
            "from `sys_role` where `id` in " +
            "(select `sys_role_id` from `sys_user_role` where `sys_user_id` = #{id} ) ")
    public List<JsRole> findRoleByUserId(@Param("id") Serializable id);

    //角色和权限中间表查询
    @Select("select `sys_permission_id` from `sys_role_permission` where `sys_role_id` = #{rid} ")
    public List<String> findPermissionByRoleid(@Param("rid") Serializable rid);

    //角色和权限中间表的增加
    @Insert("insert into `sys_role_permission` ( `sys_role_id` , `sys_permission_id` ) values ( #{rid} , #{pid} )")
    @Options(useGeneratedKeys = true, keyProperty = "id", keyColumn = "id")
    public int insertPermissionByRoleId(@Param("rid") Serializable rid, @Param("pid") Serializable pid);

    //角色和权限中间表的删除
    @Delete("delete from `sys_role_permission` where `sys_role_id` = #{rid} ")
    public int deletePermissionByRoleId(@Param("rid") Serializable rid);

    //角色和权限中间表的删除（单个）
    @Delete("delete from `sys_role_permission` where `sys_role_id` = #{rid} and `sys_permission_id` = #{pid} ")
    public int deletePermissionByRoleAndPId(@Param("rid") Serializable rid, @Param("pid") Serializable pid);

}
